<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" @click="handleAdd"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleAdd"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleAdd"
          >导出</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="roleList"
      @seletion-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
      <el-table-column
        label="角色编号"
        prop="id"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        label="角色名称"
        prop="name"
        align="center"
        :show-overflow-tooltip="true"
        width="150"
      ></el-table-column>
      <el-table-column
        label="权限字符"
        prop="perms"
        :show-overflow-tooltip="true"
        width="150"
      ></el-table-column>
      <el-table-column
        label="显示顺序"
        prop="sort"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column label="状态" prop="" width="120">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="create_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span> </template
        >Data
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :hide-on-single-page="true"
      :page-size="pageSize"
      :current-page="pageNum"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="权限字符" prop="perms">
          <el-input v-model="form.perms" placeholder="请输入权限字符"></el-input>
        </el-form-item>
        <el-form-item label="角色顺序" prop="sort">
          <el-input-number
            v-model="form.sort"
            controls-position="right"
            :min="0"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">显示</el-radio>
            <el-radio :label="0">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单权限">
          <el-tree
            :data="menuOptions"
            show-checkbox
            ref="menu"
            node-key="id"
            empty-text="加载中，请稍候"
            :props="defaultProps"
          ></el-tree>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { roleList, treeSelect, addRole, editRole } from "@/api/system/role";
export default {
  name: "Roles",
  data() {
    return {
      loading: false,
      total: 0,
      roleList: [],
      title: "",
      open: false,
      menuOptions: [],
      pageNum: 1,
      pageSize: 10,
      form: {},
      defaultProps: {
        children: "children",
        label: "title",
      },
      rules: {
        roleName: [{ require: true, message: "角色名称不能为空", trigger: "blur" }],
        roleKey: [{ require: true, message: "权限字符不能为空", trigger: "blur" }],
        roleSort: [{ require: true, message: "角色顺序不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleAdd() {
      this.reset();
      this.getMenuTreeselect();
      this.open = true;
      this.title = "添加角色";
    },
    handleUpdate(row) {
      this.reset();
      const role_menu = this.getRoleMenuTreeselect(row.id);
      //   this.form = deepClone(row);
      this.open = true;
      role_menu.then((res) => {
        this.$refs.menu.setCheckedKeys(res.checkedKeys);
      });
    },
    handleDelete(row) {
      console.log(row);
      //   this.$confirm('是否确认删除角色编号为"' + row.id + '"的数据项?', "警告", {
      //     confirmButtonText: "确定",
      //     cancelButtonText: "取消",
      //     type: "warning",
      //   })
      //     .then(function () {
      //       return deleteRole({ id: this.row.id });
      //     })
      //     .then(() => {
      //       this.getList();
      //       this.msgSuccess("删除成功");
      //     })
      //     .catch(function (err) {
      //       this.msgError(err.msg);
      //     });
    },
    getMenuTreeselect() {
      treeSelect().then((res) => {
        this.menuOptions = res.data;
      });
    },
    getList() {
      let data = {
        listRows: this.pageSize,
        page: this.pageNum,
      };
      roleList(data).then((res) => {
        if (res.success) {
          this.roleList = res.data;
        }
      });
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            this.role_menu = this.getMenuAllCheckedKeys();
            editRole(this.form).then((res) => {
              if (res.success) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            this.form.role_menu = this.getMenuAllCheckedKeys();
            addRole(this.form).then((res) => {
              if (res.success) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    getMenuAllCheckedKeys() {
      let checkedKeys = this.$refs.menu.getHalfCheckedKeys();

      let halfCheckedKeys = this.$refs.menu.getCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.form = {
        roleId: undefined,
        roleName: undefined,
        roleKey: undefined,
        roleSort: 0,
        status: "1",
        remark: undefined,
      };
      this.resetForm("form");
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    handleSelectionChange(selection) {
      console.log(selection);
      //
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.getList();
    },
  },
};
</script>
